from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from db_manage.prompt_manage import save_prompt, get_latest_prompt, get_prompts
from database import get_db
from sqlalchemy.orm import Session

router = APIRouter(prefix="/api", tags=["prompts"])

class Prompt(BaseModel):
    prompt_text: str

@router.post("/save-prompt")
async def api_save_prompt(
    prompt: Prompt,
    db: Session = Depends(get_db)
):
    result = save_prompt(db, prompt.prompt_text, 'admin')
    if "error" in result:
        raise HTTPException(status_code=400, detail=result["error"])
    return result

@router.get("/get-latest-prompt")
async def api_get_latest_prompt(
    db: Session = Depends(get_db)
):
    result = get_latest_prompt(db)
    if "error" in result:
        raise HTTPException(status_code=404, detail=result["error"])
    return result

@router.get("/get-prompts")
async def api_get_prompts(
    db: Session = Depends(get_db)
):
    return get_prompts(db)